/*
 * @Author: ischen
 * @Date: 2024-06-11 11:37:17
 * @LastEditTime: 2024-06-13 11:25:02
 * @FilePath: \新建文件夹\xx8563\PCF8563.c
 * @Description: 16C554 Driver
 * ischen.x@outlook.com
 * Copyright (c) 2024 by ischen.x@outlook.com, All Rights Reserved. 
 */
#include "PCF8563.h"

static uint8_t dec_to_bcd(uint8_t dec)
{
    return (dec / 10 * 16 + dec % 10);
}

static uint8_t bcd_to_dec(uint8_t bcd)
{
    return (10 * (bcd>>4) + (bcd & 0x0F));
}

/**
 * @description: PCF8563 IIC通讯接口，写操作
 * @param {uint8_t} reg_addr 目标寄存器地址
 * @param {uint8_t} *data 数据存储地址
 * @param {uint8_t} length 读取数据长度
 * @return {int} 0: 写入成功
 */
static int pcf8563_i2c_mem_write(uint8_t reg_addr, uint8_t *data, uint8_t length)
{
    return sw_i2c_mem_write(&i2c_interface, PCF8563_IIC_ADDR, reg_addr, data, length);
}

/**
 * @description: PCF8563 IIC通讯接口，读操作
 * @param {uint8_t} reg_addr 目标寄存器地址
 * @param {uint8_t} *data 数据存储地址
 * @param {uint8_t} length 读取数据长度
 * @return {int} 0：读取成功
 */
static int pcf8563_i2c_mem_read(uint8_t reg_addr, uint8_t *data, uint8_t length)
{
    return sw_i2c_mem_read(&i2c_interface, PCF8563_IIC_ADDR, reg_addr, data, length);
}

/**
 * @description: 获取时间
 * @param {tm} *time_struct
 * @return {int} 0：获取成功
 */
int pcf8563_get_time(struct tm *time_struct)
{    
    int ret = 0;
    pcf8563_reg_type reg = {0};
    ret = pcf8563_i2c_mem_read(0x02, (uint8_t *)&reg + 2, 7);
    if (ret == 0){
        time_struct->tm_sec =  bcd_to_dec(reg.VL_seconds&0x7F);
        time_struct->tm_min =  bcd_to_dec(reg.Minutes);
        time_struct->tm_hour=  bcd_to_dec(reg.Hours&0x3F);
        time_struct->tm_wday=  bcd_to_dec(reg.Weekdays&0x03);
        time_struct->tm_mon =  bcd_to_dec(reg.Century_mounths&0x1F) - 1;
        time_struct->tm_mday = bcd_to_dec(reg.Days&0x3F);
        time_struct->tm_year = bcd_to_dec(reg.Years) + 100; 
    } else {
        time_struct = NULL;
        return -2;
    }
    if ((reg.VL_seconds & 0x80) != 0){
        ret = -1;
    }
    return ret;
}

/**
 * @description: 设置pcf8563时间
 * @param {tm} *time_struct
 * @return {*}
 */
int pcf8563_set_time(struct tm *time_struct)
{
    pcf8563_reg_type PCF8563 = {
        0x00,
        0x00,
        dec_to_bcd(time_struct->tm_sec ),
        dec_to_bcd(time_struct->tm_min ),
        dec_to_bcd(time_struct->tm_hour ),
        dec_to_bcd(time_struct->tm_mday ),
        dec_to_bcd(time_struct->tm_wday ),
        dec_to_bcd(time_struct->tm_mon  + 1),
        dec_to_bcd(time_struct->tm_year - 100),
    };
    return pcf8563_i2c_mem_write(0x02, (uint8_t *)&PCF8563 + 2, 7);
}

#if PCF8563_DEBUG
/**
 * @description: 获取PCF8563寄存器数据
 * @param {pcf8563_reg_type} *reg 存入地址
 * @return {*}
 */
int pcf8563_get_reg(pcf8563_reg_type *pdata)
{
    return pcf8563_i2c_mem_read(0x00, pdata, sizeof(pcf8563_reg_type));
}

/**
 * @description: 设置PCF8563寄存器数据
 * @param {pcf8563_reg_type} *reg 存入地址
 * @return {*}
 */
int pcf8563_set_reg(pcf8563_reg_type *pdata)
{
    return pcf8563_i2c_mem_write(0x00, pdata, sizeof(pcf8563_reg_type));
}
#endif
